package my.edu.model.bills.sales.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.common.constant.MessageConstant;
import my.edu.common.exception.Order.PurchaseException;
import my.edu.common.exception.Order.SalesException;
import my.edu.model.bills.purchase.entity.PurchaseOrder;
import my.edu.model.bills.purchase.entity.PurchaseOrderDetail;
import my.edu.model.bills.sales.entity.SalesOrder;
import my.edu.model.bills.sales.entity.dto.SalesDetailDTO;
import my.edu.model.bills.sales.entity.dto.SalesOrderSearchDTO;
import my.edu.utils.OrderDetailServiceUtil;
import my.edu.utils.OrderServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import my.edu.model.bills.sales.entity.SalesOrderDetail;
import my.edu.model.bills.sales.mapper.SalesOrderDetailMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SalesOrderDetailServiceImpl extends ServiceImpl<SalesOrderDetailMapper, SalesOrderDetail> implements ISalesOrderDetailService{

    @Autowired
    private OrderServiceUtil orderServiceUtil;

    @Override
    @Transactional
    public void saveBatchByDTO(List<SalesDetailDTO> salesDetailDTOList,Long salesOrderId) {
        List<SalesOrderDetail> purchaseOrderDetails = salesDetailDTOList.stream().map(i -> {
            SalesOrderDetail detail = BeanUtil.copyProperties(i, SalesOrderDetail.class);
            //设置销售单id
            detail.setSalesOrderId(salesOrderId);
            return detail;
        }).toList();
        saveBatch(purchaseOrderDetails);
    }

    @Override
    @Transactional
    public void updateBatchByDTO(List<SalesDetailDTO> salesDetailDTOList, Long id) {
        remove(Wrappers.<SalesOrderDetail>lambdaQuery().eq(SalesOrderDetail::getSalesOrderId,id));
        saveBatchByDTO(salesDetailDTOList,id);
    }

    @Override
    public List<SalesOrderDetail> getReportList(SalesOrderSearchDTO salesOrderSearchDTO) {
        MPJLambdaWrapper<SalesOrderDetail> wrapper = new MPJLambdaWrapper<SalesOrderDetail>()
                .selectAll(SalesOrderDetail.class)
                .leftJoin(SalesOrder.class, SalesOrder::getId, SalesOrderDetail::getSalesOrderId)
                .between(salesOrderSearchDTO.getBeginTime() != null,
                        SalesOrder::getOrderDate, salesOrderSearchDTO.getBeginTime(), salesOrderSearchDTO.getEndTime())
                .eq(salesOrderSearchDTO.getCustomerId()!= null,
                        SalesOrder::getCustomerId, salesOrderSearchDTO.getCustomerId())
                .and(StringUtils.hasText(salesOrderSearchDTO.getMaterialParam()), i->i
                        .like(SalesOrderDetail::getProductNo, salesOrderSearchDTO.getMaterialParam())
                        .or()
                        .like(SalesOrderDetail::getProductName, salesOrderSearchDTO.getMaterialParam()));
        List<SalesOrderDetail> salesOrderDetails = baseMapper.selectJoinList(wrapper);
        return salesOrderDetails;
    }
}
